package cn.bornsoft.mobileServer.db;

import java.sql.Timestamp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * JkNote entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.bornsoft.mobileServer.db.JkNote
 * @author MyEclipse Persistence Tools
 */

public class JkNoteDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JkNoteDAO.class);
	// property constants
	public static final String CLASSID = "classid";
	public static final String NAME = "name";
	public static final String ADRESS = "adress";
	public static final String EMAIL = "email";
	public static final String TEL = "tel";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String REPLAYCONTENT = "replaycontent";
	public static final String REPLAYUSERNAME = "replayusername";
	public static final String REPLAYUSERID = "replayuserid";
	public static final String REPLAYDEPATENAME = "replaydepatename";
	public static final String IP = "ip";
	public static final String STATE = "state";
	public static final String ISDELETE = "isdelete";
	Transaction tx = null;

	public void save(JkNote transientInstance) {
		log.debug("saving JkNote instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(JkNote persistentInstance) {
		log.debug("deleting JkNote instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public JkNote findById(java.lang.String id) {
		log.debug("getting JkNote instance with id: " + id);
		try {
			JkNote instance = (JkNote) getSession().get(
					"cn.bornsoft.mobileServer.db.JkNote", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(JkNote instance) {
		log.debug("finding JkNote instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.bornsoft.mobileServer.db.JkNote").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByKey(Object value, int max, int page) {
		log
				.debug("SELECT * FROM Jk_Note as model WHERE (model.name like ? or model.title like ? or model.content like ? )and  model.isdelete=0 and model.state=1  order by model.adddate desc");
		try {
			// String queryString = "from JkNote as model where model."
			// + propertyName +
			// "= ? and  model.isdelete=0 and model.state=1  order by model.adddate desc";
			getSession().clear();// 清除查询缓存
			Query query = getSession()
					.createSQLQuery(
							"SELECT * FROM Jk_Note as model WHERE (model.name like ? or model.title like ? or model.content like ? )and  model.isdelete=0 and model.state=1  order by model.adddate desc")
					.addEntity(JkNote.class);
			query.setString(0, "%" + value + "%");
			query.setString(1, "%" + value + "%");
			query.setString(2, "%" + value + "%");
			query.setFirstResult(max * (page - 1));//
			query.setMaxResults(max);
			List list = query.list();

			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding JkNote instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from JkNote as model where model."
					+ propertyName
					+ "= ? and  model.isdelete=0 and model.state=1  order by model.adddate desc";
			getSession().clear();// 清除查询缓存
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByClassid(Object classid) {
		return findByProperty(CLASSID, classid);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByAdress(Object adress) {
		return findByProperty(ADRESS, adress);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByReplaycontent(Object replaycontent) {
		return findByProperty(REPLAYCONTENT, replaycontent);
	}

	public List findByReplayusername(Object replayusername) {
		return findByProperty(REPLAYUSERNAME, replayusername);
	}

	public List findByReplayuserid(Object replayuserid) {
		return findByProperty(REPLAYUSERID, replayuserid);
	}

	public List findByReplaydepatename(Object replaydepatename) {
		return findByProperty(REPLAYDEPATENAME, replaydepatename);
	}

	public List findByIp(Object ip) {
		return findByProperty(IP, ip);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByIsdelete(Object isdelete) {
		return findByProperty(ISDELETE, isdelete);
	}

	public List findAll(int max, int page) {
		log.debug("finding all JkNote instances");
		try {
			String queryString = "from JkNote as model where model.isdelete=0 and model.state=1 order by model.adddate desc";
			getSession().clear();// 清除查询缓存

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(max * (page - 1));//
			queryObject.setMaxResults(max);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public JkNote merge(JkNote detachedInstance) {
		log.debug("merging JkNote instance");
		try {
			getSession().clear();// 清除查询缓存
			tx = getSession().beginTransaction();

			JkNote result = (JkNote) getSession().merge(detachedInstance);
			getSession().flush();

			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(JkNote instance) {
		log.debug("attaching dirty JkNote instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(JkNote instance) {
		log.debug("attaching clean JkNote instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}